//
//  SEGTarget_OpenDoor.m
//  SEGUhomeIntelligentHardware
//
//  Created by Chiang on 2018/4/17.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGTarget_OpenDoor.h"
//Controller
#import "SEGElevatorControlViewController.h"
#import "SEGDoorBaseController.h"
#import "SEGCommonAllDoorListViewController.h"
#import "SEGCommonDoorViewController.h"
#import "SEGSanyDoorBaseViewController.h"
//View
#import "SEGDoorNoCooperationOrCertificationView.h"
//Model
#import "SEGOpenDoorBussinessManager.h"
#import "SEGCommonConfig.h"
#import "SEGNewOpenDoorModel.h"
//ViewModel
#import <SEGViewModel/SEGDoorListViewModel.h>


typedef void(^SEGOpenDoorSuccessCallBack)(NSDictionary *info);
typedef void(^SEGDoorVCDisappearCallBack)(void);
typedef void(^DoorListCallBack)(NSArray *doorList);
typedef void(^SEGSanyDoorVCCreateBlock)(UIViewController *); ///< sany创建门禁列表的block

@interface SEGTarget_OpenDoor ()
@property (nonatomic, strong) SEGDoorListViewModel *listViewModel;
@end

@implementation SEGTarget_OpenDoor


/// 统一处理门禁跳转
/// @param params params
- (UIViewController*)segAction_showDoorWithParams:(NSDictionary *)params {
    SEGEnumOpendoorShowType type = [SEGProfileManager openDoorType];
    SEGOpenDoorSuccessCallBack openDoorSuccessCallback = params[@"showedCallBack"];
    SEGDoorVCDisappearCallBack disappearCallBack = params[@"disappearCallBack"];
    
    /*
     从哪触发的门禁
     0: 点击tab
     -1: -1代表是从常用门禁控件进入
     1: 从菜单配置进入 action OPEN_DOOR_LIST
     */
    NSInteger fromType = [params[@"showType"] integerValue];

    // 用户没有权限的时候，点击门禁，从菜单进来会弹出Alter，其余情况弹出半截认证弹窗，不区分业态
    if (type == SEGEnumOpendoorShowType_NoCooperation || type == SEGEnumOpendoorShowType_NoCertification) {
        if (fromType == 1) {
            SEGNewAlertView *alertView = [[SEGNewAlertView alloc] init];
            [alertView showWithAlertTitle:nil text:SEGBasicBussinessStringDefine.promptUserAuthentication buttonTitlesArray:@[@"取消", @"申请认证"] actionBlock:^(NSInteger btnIndex) {
                if (btnIndex == 1) {
                    UIViewController *pushVC = [[SEGUhomeMediator sharedInstance] seg_getCurrentController:nil];
                    [pushVC.navigationController pushViewController:[[SEGUhomeMediator sharedInstance] seg_initApplyAuthorViewController:nil] animated:YES];
                }
            }];
        } else {
            [SEGDoorNoCooperationOrCertificationView show];
        }
        return nil;
    }
    
    // 「 开启老门禁配置的项目」使用老的常用门
    if (SEGCommonConfig.shared.isUseOldDoor) {
        NSArray *commonDoors = [self.listViewModel getOldCommonDoor];
        if (commonDoors.count > 0) {
            SEGCommonDoorViewController *commonDoorVC = [[SEGCommonDoorViewController alloc] init];
            commonDoorVC.disapperCallback = disappearCallBack;
            commonDoorVC.completedCallback = ^(SEGNewOpenDoorModel * _Nonnull data) {
                NSDictionary *info = [data transform_InTo_Dictionary];
                openDoorSuccessCallback(info);
            };
            return commonDoorVC;
        }

        SEGCommonAllDoorListViewController *commonListVC = [[SEGCommonAllDoorListViewController alloc] init];
        commonListVC.disapperCallback = disappearCallBack;
        commonListVC.completedCallback = ^(SEGNewOpenDoorModel * _Nonnull data) {
            NSDictionary *info = [data transform_InTo_Dictionary];
            openDoorSuccessCallback(info);
        };
        return commonListVC;
    }
    
    /*
     将以前showType传参的逻辑移除了，直接使用[SEGSystemAccountManager openDoorType]来获取
     这里添加showType作为标识以区分是否从服务页面进入逻辑，-1代表是从常用门禁控件进入
     */
    BOOL isFromServerList = fromType == -1;
    SEGDoorBaseController *doorVC = [[SEGDoorBaseController alloc] initWithDoorType:type isFromServerList:isFromServerList completed:^(SEGNewOpenDoorModel * data) {
        NSDictionary *info = [data transform_InTo_Dictionary];
        openDoorSuccessCallback(info);
    }];
    return doorVC;
}

/**
 * 初始化梯控界面
 * @author Sam
 */
- (UIViewController*)segAction_initElevatorControlViewController:(NSDictionary *)params{
    SEGElevatorControlViewController *VC = [[SEGElevatorControlViewController alloc]init];
    return VC;
}

/**
 获取前八个门禁 先返回本地数据,请求网络后，再返回新排序后的数据
 */
- (void)segAction_getRecentDoorListWithCallBack:(NSDictionary *)params {
    DoorListCallBack doorListCallBack = params[@"callBack"];
    // 会先展示本地数据，加载完网络数据后再展示网络数据
    [self.listViewModel requestDoorComponentList:^(NSArray<NSDictionary *> *doorlist) {
        if (doorListCallBack) {
            doorListCallBack(doorlist);
        }
    }];
}


/**
 点击某个门禁 触发开门事件

 @param params 点击门禁的数据
 */
- (void)segAction_openDoorWithParams:(NSDictionary *)params {
    NSDictionary *doorDic = params[@"doorDic"];
    SEGNewMyDoorListModel* doorModel = [SEGNewMyDoorListModel yy_modelWithJSON:doorDic];
    void(^callback)(NSDictionary *) = params[@"callback"];
    [[SEGOpenDoorBussinessManager shareInstance] openDoorWithDoorModel:doorModel completeCallback:^(SEGNewOpenDoorModel * _Nonnull data) {
        NSDictionary *info = [data transform_InTo_Dictionary];
        callback(info);
    }];
}

/**
 上传历史的开门纪录
 */
- (void)segAction_submitOpenDoorRecordHistory:(NSDictionary *)params {
    [self.listViewModel submitOpenDoorHistory];
}

- (SEGDoorListViewModel *)listViewModel {
    if (!_listViewModel) {
        _listViewModel = [[SEGDoorListViewModel alloc] init];
    }
    return _listViewModel;
}

@end
